﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using KL.UFrame.Api.Extensions;
using KL.UFrame.Core.Result;
using KL.UFrame.Data.Business;
using KL.UFrame.Data.Entities;
using KL.UFrame.Data.Models;
using Microsoft.AspNet.Identity;

namespace KL.UFrame.Api.ApiControllers
{
    [RoutePrefix("api/Trainning")]
    public class TrainningController : K9ApiBaseController
    {

        private ITrainningBusiness _business;

        public TrainningController(ITrainningBusiness business)
        {
            _business = business;
        }

        public async Task<ResultMessage<List<TrainningModel>>> Get(int categoryId,bool onlyEnabled)
        {
            var model = await _business.GetAllTrainningsAsync(categoryId, onlyEnabled);
            return model;
        }

        [HttpPost]
        [Route("Delete")]
        public async Task<ResultMessage<bool>> Delete(int id)
        {
            return new ResultMessage<bool>(await _business.DeleteTrainningAsync(id));
        }

        [HttpPost]
        [Route("Resume")]
        public async Task<ResultMessage<bool>> Resume(int id)
        {
            return new ResultMessage<bool>(await _business.ResumeTrainningAsync(id));
        }

        [Route("Update")]
        public async Task<ResultMessage> Update(TrainningModel model)
        {
            if (!ModelState.IsValid)
            {
                return ModelState.ToResultMessage();
            }

            model.UpdateId = User.Identity.GetUserId();
            model.UpdateTime = DateTime.Now; ;
            var id = await _business.UpdateTrainningAsync(model);
            return new ResultMessage(id);
        }

        [Route("Create")]
        public async Task<ResultMessage> Create(TrainningModel model)
        {
            if (!ModelState.IsValid)
            {
                return ModelState.ToResultMessage();
            }

            model.CreateId = User.Identity.GetUserId();
            model.CreateTime = DateTime.Now; ;
            var id = await _business.CreateTrainningAsync(model);
            return new ResultMessage(id);
        }

        [Route("Search")]
        public async Task<ResultMessage<List<EnrollModel>>>  GetEnrolls(int trainningId, string phone, DateTime start, DateTime end, int pageIndex, int pageSize)
        {
            return await _business.SearchEnrollsAsync(trainningId, phone, start.ToBeginning(), end.ToEnd(), pageIndex, pageSize);
        }

        [Route("Audit")]
        public async Task<ResultMessage> Audit(int enrollId, string statusId, string description)
        {
            var status = EnrollStatus.New;

            if (!Enum.TryParse(statusId, out status))
            {
                return new ResultMessage(-1, "状态参数不正确");
            }

            if (status == EnrollStatus.Failed && string.IsNullOrEmpty(description))
            {
                return new ResultMessage(-2, "请输入不通过的原因");
            }
            var result = await _business.AuditAsync(enrollId, status, description);
            return result ? new ResultMessage() : new ResultMessage(-3, "审核失败，请稍后再试");
        }
    }
}
